package com.weiyi.weiman.utils;

import com.weiyi.weiman.domain.dto.ResultInfo;
import com.weiyi.weiman.domain.status.ResultStatus;
import com.weiyi.weiman.domain.status.TokenStatus;

import java.util.Objects;

/**
 * @Author zhang
 * @Date 2021/4/27 下午5:13
 * @Description 设置返回信息的工具类
 * @Version 1.0
 **/
public class ResultUtils {

    public static ResultInfo successInfo(Object data) {
        return new  ResultInfo(ResultStatus.SUCCESS.code, ResultStatus.SUCCESS.message, data);
    }

    public static ResultInfo failureInfo(ResultStatus status) {
        if (!Objects.isNull(status))
            return failureInfo(status.getCode(), status.getMessage());
        return failureInfo("未知错误");
    }

    public static ResultInfo failureInfo(TokenStatus status) {
        if (!Objects.isNull(status))
            return failureInfo(status.getCode(), status.getMessage());
        return failureInfo("未知错误");
    }

    public static ResultInfo failureInfo(String message) {
        return new ResultInfo(4000, message);
    }

    private static ResultInfo failureInfo(Integer code, String message) {
        if (Objects.isNull(code)) return failureInfo(message);
        return new ResultInfo(code, message);
    }

}
